package com.longrise.android.byjk.plugins.aboutme.personalInfo.product_show;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sujizhong on 2016/12/14.
 */

public class ProductAdapter extends BaseAdapter {

    private class ProductName {
        private TextView mTextName;
        private View mViewLine;
    }

    private int mCount = 0;

    private List<EntityBean> mList = new ArrayList<>();

    private Context mCxt = null;

    public ProductAdapter(Context cxt) {
        this.mCxt = cxt;
    }

    public void setData(EntityBean[] been) {
        if (mList.size() > 0) {
            mList.clear();
        }
        for (EntityBean bean : been) {
            mList.add(bean);
        }
    }

    @Override
    public int getCount() {
        mCount = mList.size();
        return mCount;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProductName productName = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mCxt).inflate(R.layout.product_item, null, false);
            productName = new ProductName();
            productName.mTextName = (TextView) convertView.findViewById(R.id.productname_tv);
            productName.mViewLine = convertView.findViewById(R.id.product_viewline);
            convertView.setTag(productName);
        } else {
            productName = (ProductName) convertView.getTag();
        }
        EntityBean itemBean = mList.get(position);
        if (itemBean != null) {
            productName.mTextName.setText(itemBean.getString("productname"));
        }
        productName.mViewLine.setVisibility(position >= mCount ? View.GONE : View.VISIBLE);
        return convertView;
    }
}
